# Project: From Home Base to Swing States: The Evolution of Digital Advertising 
#          Strategies during the 2020 US Presidential Primary
# Authors: NaLette Brodnax and Piotr Sapiezynski

library(tidyverse)

ads_all <- read_csv('replication_data_monthly.csv')

# Candidates who dropped
drop_candidates <- c("Amy Klobuchar", "Andrew Yang", 
                     "Bernie Sanders", "Cory Booker", "Elizabeth Warren", 
                     "Joe Biden", "John K. Delaney", "Marianne Williamson", 
                     "Michael Bennet", "Mike Bloomberg", "Pete Buttigieg", 
                     "Tom Steyer", "Tulsi Gabbard")

ads_dropped <- ads_all %>% 
  filter(!candidate %in% drop_candidates) 

# Candidates who remained
drop_candidates <- c("Beto O'Rourke", "Eric Swalwell", 
                     "Governor Steve Bullock", "Jay Inslee", "John Hickenlooper",
                     "Julián Castro", "Kamala Harris", "Kirsten Gillibrand",
                     "Mayor Wayne Messam", "Mike Gravel", "Richard Ojeda",
                     "Seth Moulton", "Tim Ryan")

ads_remained <- ads_all %>% 
  filter(!candidate %in% drop_candidates)

# Fit by month (simple with no random slopes and no interactions)
mod_simple <- function(df, mo){
  data <- filter(df, month == mo)
  fit <- lm(budget_frac_per_resident ~ is_home + swing + feb + march,         
           data = data)
  return(fit)
} 

# Months to fit: Jan 2019 (month 1) thru Feb 2020 (month 14)
months <- seq(1,12,1)
all_fits_dropped <- lapply(months, function(x) mod_simple(ads_dropped, x))
all_fits_remained <- lapply(months, function(x) mod_simple(ads_remained, x))

# Convert results to dataframe
mon = tibble(month = seq(1,12))
out_dropped <- lapply(all_fits_dropped, function(x) tidy(x)) %>% 
  bind_rows() %>% 
  mutate(Candidates = 'Dropped') %>% 
  filter(term %in% c('is_home')) %>% 
  bind_cols(mon)
out_remained <- lapply(all_fits_remained, function(x) tidy(x)) %>% 
  bind_rows() %>% 
  mutate(Candidates = 'Remained') %>% 
  filter(term %in% c('is_home')) %>% 
  bind_cols(mon)

out_coef <- bind_rows(out_dropped, out_remained)

# Plot
ggplot(aes(month, estimate, shape=Candidates), data=out_coef) + geom_line() + 
  geom_point() + geom_errorbar(aes(ymin=estimate-std.error, ymax=estimate+std.error), 
                               width=.1) +
  scale_x_continuous(breaks = seq(1,12,3))
